<!--
 * @Autor: Hongting Yuan
 * @Date: 2021-12-08 21:04:13
 * @LastEditors: Hongting Yuan
 * @LastEditTime: 2021-12-16 12:57:25
 * @Description: 查询并展示论文成果信息并可以进行信息的导出
 * @FilePath: \Management-of-Scientific-Research-in-Universities-Front-master\src\views\scientificachieve\ArticleAchieve.vue
-->

<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ name:'WorkPlace' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>科研成果</el-breadcrumb-item>
      <el-breadcrumb-item>论文成果</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card
      style="width: 100%;height: 100%"
    >
      <!-- 根据用户名搜索 -->
      <el-input v-model="userName" style="margin-bottom: 15px;width: 260px; height: 40px;margin-right: 50px;" placeholder="请输入负责人名字" clearable @clear="getarticleAchieveByuserName">
        <el-button slot="append" icon="el-icon-search" @click="getarticleAchieveByuserName" />
      </el-input>
      <!-- 根据论文名搜索 -->
      <el-input v-model="paperName" style="margin-bottom: 15px;margin-right: 10px;width: 260px; height: 40px;" placeholder="请输入论文名" clearable @clear="getarticleAchieveBypaperName">
        <el-button slot="append" icon="el-icon-search" @click="getarticleAchieveBypaperName" />
      </el-input>
      <!-- 导出信息 -->
      <download-excel :data="articleAchieveList" :fields="excelFields" style="display: inline;">
        <el-button v-preventReClick="3000" type="primary" style="float: right;margin-bottom: 15px;" icon="el-icon-download">导出信息</el-button>
      </download-excel>
      <!-- 渲染数据 -->
      <el-table
        :data="articleAchieveList"
        :default-sort="{prop: 'applyTime', order: 'descending'}"
        border
        stripe
      >
        <el-table-column
          type="index"
          :show-overflow-tooltip="true"
          label="#"
          align="center"
        />
        <el-table-column
          :show-overflow-tooltip="true"
          prop="projectName"
          label="项目名"
          align="center"
        />
        <el-table-column
          prop="leader"
          :show-overflow-tooltip="true"
          label="负责人"
          align="center"
        />
        <el-table-column
          prop="paperName"
          :show-overflow-tooltip="true"
          label="论文名"
          align="center"
        />
        <el-table-column
          prop="paperId"
          label="论文编号"
          :show-overflow-tooltip="true"
          align="center"
        />
        <el-table-column
          prop="applyTime"
          label="申请时间"
          sortable
          align="center"
        />
        <el-table-column
          prop="includeNumber"
          label="收录号"
          align="center"
        />
        <el-table-column
          prop="grade"
          label="项目等级"
          align="center"
        />
        <el-table-column
          prop="departmentId"
          label="成果所属单位"
          align="center"
        />
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="text-align: right;"
        :current-page="currentPage"
        :page-sizes="[3, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>
<script>
import axios from 'axios'
export default {
  name: 'ArticleAchieve',
  data() {
    return {
      // 将表格中的信息导出为Excel的文件头
      excelFields: {
        '项目名': 'projectName',
        '项目负责人': 'leader',
        '论文名': 'paperName',
        '论文编号': 'paperId',
        '申请时间': 'applyTime',
        '收录号': 'includeNumber',
        '项目等级': 'grade',
        '成果所属单位': 'departmentId'
      },
      articleAchieveList: [],
      currentPage: 1,
      pageSize: 5,
      totalNum: 0,
      grade: ['T类', 'A类', 'B类', '其他'],
      departmentList: [],
      userName: '',
      paperName: ''
    }
  },
  created() {
    // this.getdepartmentList()
    // 获取缓存中的部门列表信息
    this.departmentList = JSON.parse(this.$store.getters.getdepartment)
    this.init()
  },
  methods: {
    /**
     * @description: 将初始化获取到的信息转化为要展示的对象数组
     * @param {*} res
     * @return {*}
     * @author: Hongting Yuan
     */
    getList(res) {
      var self = this
      for (var i = 0; i < res.data.data.list.length; i++) {
        // if (res.data.data.list[i].examineStatus !== '审核通过') {
        //   continue
        // }
        const objectResult = {}
        objectResult.projectName = res.data.data.list[i].projectExamineVo.projectName
        objectResult.leader = res.data.data.list[i].leaderName
        if (objectResult.leader === null) {
          for (var j = 0; j < res.data.data.list[i].projectExamineVo.team.length; j++) {
            if (res.data.data.list[i].projectExamineVo.team[j].userId === res.data.data.list[i].leaderId) {
              objectResult.leader = res.data.data.list[i].projectExamineVo.team[j].userRealname
            }
          }
        }
        objectResult.paperName = res.data.data.list[i].paperName
        objectResult.paperId = res.data.data.list[i].paperId
        objectResult.applyTime = self.getdata(res.data.data.list[i].applyTime)
        objectResult.grade = self.grade[res.data.data.list[i].projectExamineVo.levelId - 1]
        objectResult.includeNumber = res.data.data.list[i].includeNumber
        objectResult.departmentId = self.departmentList[res.data.data.list[i].aodId].name
        objectResult.examineStatus = res.data.data.list[i].examineStatus
        self.articleAchieveList.push(objectResult)
      }
      // self.totalNum = self.articleAchieveList.length
      self.totalNum = res.data.data.total
    },
    /**
     * @description: 获取论文成果信息，初始化论文成果数据
     * @param {*}
     * @return {*}
     * @author: Hongting Yuan
     */
    init() {
      var self = this
      axios.get(`/achievement/searchAllPass/paper?pageNum=${self.currentPage}&pageSize=${self.pageSize}`)
        .then(res => {
          if (res.data.code !== 200) return this.$message.error('获取论文列表失败')
          // console.log(res)
          self.articleAchieveList = []
          if (res.data.data && res.data.data.list) {
            self.getList(res)
          } else {
            self.totalNum = 0
          }
          // console.log(this.articleAchieveList)
        })
        .catch(err => {
          console.error(err)
        })
    },
    /**
     * @description: 根据论文负责人姓名搜索论文成果
     * @param {*}
     * @return {*}
     * @author: Hongting Yuan
     */
    getarticleAchieveByuserName() {
      var self = this
      if (self.userName === '') {
        self.init()
      } else {
        axios.get('/achievement/seaUserName/paper', { params: { userName: self.userName }})
          .then(res => {
            if (res.data.code !== 200) return this.$message.error('搜索论文信息失败')
            self.articleAchieveList = []
            if (res.data.data && res.data.data.list) {
              self.getList(res)
            } else {
              self.totalNum = 0
            }
            // console.log(this.articleAchieveList)
          })
          .catch(err => {
            console.error(err)
          })
      }
    },
    /**
     * @description: 根据论文名搜索论文成果
     * @param {*}
     * @return {*}
     * @author: Hongting Yuan
     */
    getarticleAchieveBypaperName() {
      var self = this
      if (self.paperName === '') {
        self.init()
      } else {
        axios.get('/achievement/searchByName/paper', { params: { achievementName: self.paperName }})
          .then(res => {
            if (res.data.code !== 200) return this.$message.error('搜索论文信息失败')
            self.articleAchieveList = []
            if (res.data.data && res.data.data.list) {
              self.getList(res)
            } else {
              self.totalNum = 0
            }
            // console.log(this.articleAchieveList)
          })
          .catch(err => {
            console.error(err)
          })
      }
    },
    /**
     * @description: 将时间戳转为2021-11-07 21:31:13格式
     * @param {*} data
     * @return {*}
     * @author: Hongting Yuan
     */
    getdata(data) {
      var date = new Date(data)
      var Y = date.getFullYear() + '-'
      var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
      return Y + M + D
    },
    // 获取部门列表
    // getdepartmentList() {
    // this.departmentList = this.$store.getters.getdepartment
    // axios.get('/dictionary/searchAll/department')
    //   .then(res => {
    //     if (res.data.code !== 200) return this.$message.error('获取部门列表失败')
    //     this.departmentList = res.data.data
    //     console.log(this.departmentList)
    //     this.init()
    //   })
    //   .catch(err => {
    //     console.error(err)
    //   })
    // },
    /**
     * @description: 页面展示数据条数发生变化
     * @param {*} val
     * @return {*}
     * @author: Hongting Yuan
     */
    handleSizeChange(val) {
      this.pageSize = val
      this.init()
    },
    /**
     * @description: 当前显示页发生变化
     * @param {*} val
     * @return {*}
     * @author: Hongting Yuan
     */
    handleCurrentChange(val) {
      this.currentPage = val
      this.init()
    }
  }
}
</script>
<style lang="less">
  .selectAuthor{
    padding-top: 20px;
    text-align: center;
  }
  .el-table-column{
    text-align: center;
  }
</style>
